﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using Newtonsoft.Json;
//using Newtonsoft.Json.Linq;

namespace wubi86
{
    public partial class FrmMain : Form
    {
        public static int inputCnt;
        public static int errCnt;
        public static DateTime startTime = DateTime.MinValue;
        private int totalWord;
        public FrmMain() {
            InitializeComponent();
            toolStripStatusLabel1.Text = "就绪";
            this.WindowState = FormWindowState.Maximized;
        }

        private void Open(int flag) {
            try {
                this.Cursor = Cursors.WaitCursor;
                Clear();
                toolStripStatusLabel1.Text = "正在加载文章内容...";
                Application.DoEvents();
                startTime = DateTime.MinValue;
                inputCnt = 0;
                string txt = "";
                if (flag == 0)
                    txt = Properties.Resources._0;
                else if (flag == 1)
                    txt = Properties.Resources._1;
                else if (flag == 2)
                    txt = Properties.Resources._2;
                else if (flag == 3)
                    txt = Clipboard.GetText();
                if (txt == "")
                    return;
                txt = txt.Replace(" ", "").Replace("\r", "").Replace("\n", "").Replace("\r\n", "").Replace("\t","");
                char[] arr = txt.ToCharArray();
                int l = txt.Length;
                totalWord = l;
                int ll = 0;
                panel1.SuspendLayout();
                for (int i = 0; i <= l - 1; i++) {
                    Control c;
                    UserControl1 uc = new UserControl1(arr[i].ToString());
                    if (panel1.Controls.Count == 0) {
                        uc.Left = 10;
                        uc.Top = 10;
                    } else {
                        c = panel1.Controls[panel1.Controls.Count - 1];

                        if (ll < panel1.Width - 52) {
                            uc.Left = c.Right + 5;
                            uc.Top = c.Top;
                        } else {
                            uc.Left = 10;
                            uc.Top = (c.Bottom);
                        }
                    }
                    uc.Tag = i;
                    uc.SetNextFocuse = new SetNextFocuse(SetNextControlFocuse);
                    uc.CountWord = new CountWord(DoCountWord);
                    panel1.Controls.Add(uc);
                    c = panel1.Controls[panel1.Controls.Count - 1];
                    ll = c.Right;
                    toolStripStatusLabel1.Text = $"正在加载文章内容...{(((double)i / (double)totalWord) * 100).ToString("F0")}%";
                   // panel1.ScrollControlIntoView(uc);
                    Application.DoEvents();
                }
                panel1.ResumeLayout();
                panel1.PerformLayout();
                if (panel1.Controls.Count > 0) {
                    panel1.ScrollControlIntoView(panel1.Controls[0]);
                    panel1.Controls[0].Focus();
                }
            } finally {
                toolStripStatusLabel1.Text = "就绪";
                this.Cursor = Cursors.Default;
            }
        }

        private void SetNextControlFocuse(int idx) {
            if (idx>=0 && idx <= panel1.Controls.Count - 1)
                panel1.Controls[idx].Focus();
        }
        
        private void DoCountWord(int errCnt,int inputCnt) {
            if (totalWord > 0) {
                toolStripStatusLabel1.Text = $"速度：{inputCnt} 个字/分钟；错误率：{(((double)errCnt / (double)totalWord) * 100).ToString("F1") + "%"}";
            }
        }
        private void Clear() {
            panel1.Controls.Clear();
            totalWord = 0;
            errCnt = 0;
            toolStripStatusLabel1.Text = "就绪";
            startTime = DateTime.MinValue;
            inputCnt = 0;
            //label1.Text = "";
            Application.DoEvents();
        }

        private void article1ToolStripMenuItem_Click(object sender, EventArgs e) {
            label1.Text = article1ToolStripMenuItem.Text;
            Open(0);           
        }

        private void article2ToolStripMenuItem_Click(object sender, EventArgs e) {
            label1.Text = article2ToolStripMenuItem.Text;
            Open(1);
        }

        private void article3ToolStripMenuItem_Click(object sender, EventArgs e) {
            label1.Text = article3ToolStripMenuItem.Text;
            Open(2);
        }

        private void copyFromClipboardToolStripMenuItem_Click(object sender, EventArgs e) {
            label1.Text = copyFromClipboardToolStripMenuItem.Text;
            Open(3);
        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e) {
            label1.Text = "";
            Clear();          
        }

        private void wubicodeToolStripMenuItem_Click(object sender, EventArgs e) {
            FrmWubiCode frmWubiCode = new FrmWubiCode();
            frmWubiCode.StartPosition = FormStartPosition.CenterParent;
            frmWubiCode.ShowDialog(this);
        }

        private void aboutmeToolStripMenuItem_Click(object sender, EventArgs e) {
            FrmAbout frmAbout = new FrmAbout();
            frmAbout.StartPosition = FormStartPosition.CenterParent;
            frmAbout.ShowDialog(this);
        }

        private void offerToolStripMenuItem_Click(object sender, EventArgs e) {
            FrmOffer frmOffer = new FrmOffer();
            frmOffer.StartPosition = FormStartPosition.CenterParent;
            frmOffer.ShowDialog(this);
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e) {
            label1.Text = copyFromClipboardToolStripMenuItem.Text;
            Open(3);
        }

        private void clearToolStripMenuItem1_Click(object sender, EventArgs e) {
            label1.Text = "";
            Clear();
        }
    }
}
